
<template>
  <div class="execution">
    <basic-container>
      <avue-crud
        :permission="permissionList"
        v-bind="bindVal"
        v-on="onEvent"
        :page.sync="page"
      >
        <template slot="menu" slot-scope="scope">
          <el-button
            v-if="permissionList.editBtn && (scope.row.reviewStatus == 0 || scope.row.reviewStatus == 3)"
            type="text" size="small"
            icon="el-icon-edit"
            @click="handleEdit(scope.row, scope.$index)">
            编辑
          </el-button>
        </template>
        <template slot="menuLeft">
          <el-button v-if="permissions.stm_swstcycleapply_submit" type="primary" size="small" @click="submitBatch">批量提交</el-button>
          <el-button v-if="permissions.stm_swstcycleapply_del" type="primary" size="small" @click="delBatch">删除</el-button>
        </template>
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import {
  fetchList,
  getObj,
  addObj,
  putObj,
  delObj,
  subBatch,
  appBatch,
  delBatchObj
} from "@/api/subwaystm/swstcycleapply";
import { tableOption,startDate, endDate } from "@/const/crud/subwaystm/swstcycleapply";
import { mapGetters } from "vuex";
import baseCrud from '@/mixins/base-crud'

export default {
  name: "swstcycleapply",
  mixins: [baseCrud('subwaystm/swstcycleapply')],
  data() {
    return {
      visible: false,
      searchForm: {
        dates1: [startDate, endDate]
      }
    };
  },
  computed: {
    ...mapGetters(["permissions"]),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permissions.stm_swstcycleapply_add, false),
        delBtn: this.vaildData(this.permissions.stm_swstcycleapply_del, false),
        editBtn: this.vaildData(this.permissions.stm_swstcycleapply_edit, false)
      };
    }
  },
  methods: {
    submitBatch() {
      var tableSelect = this.$refs.crud.tableSelect
      if (tableSelect.length == 0) {
        this.$message.info("请选择要提交的记录");
        return false;
      }
      for (const table of this.$refs.crud.tableSelect) {      
	if (table.reviewStatus != "0") {
          this.$message.error("请检查数据，只能选择未提交的");
          return false;
        }
      }
      this.$confirm("确认提交?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        subBatch(tableSelect)
          .then(data => {
            this.$message.success("提交成功");
            this.getList(this.page);
          })
      }); 
    },
    delBatch: function () {
      if (this.$refs.crud.tableSelect.length < 1) {
        this.$message.error("请勾选数据");
        return false;
      }
      let ids = [];
      for (const table of this.$refs.crud.tableSelect) {
       /*
	if (table.reviewStatus != "0") {
          this.$message.error("请检查数据，只能删除未提交的");
          return false;
        }*/
        ids.push(table.id);
      }
      this.$confirm("确认删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        delBatchObj(ids)
          .then((data) => {
            this.$message.success("删除成功");
            this.getList(this.page);
            done();
          })
          .catch(() => {
            loading();
          });
      });  
    },
    handleEdit(row, index){
      this.$refs.crud.rowEdit(row, index)
    }
  }
};
</script>
